package com.zhou.base.components.bs.common.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

/**
 * @author xianfeng
 * @date 2023/7/21 17:57
 * @description:
 */
@Slf4j
public class RemarkUtil {

    public static String generateRemark(String old, String input) {
        String time = LocalDateTime.now().toString();
        if (StrUtil.isEmpty(old)) {
            return StrUtil.isEmpty(input) ? null : time + " " + input;
        }
        StringBuilder builder = new StringBuilder(old);
        builder.append(System.lineSeparator());
        builder.append(time + " " + input);
        return builder.toString();
    }

    public static String generateRemark(String old, String input, int strLength) {
        String time = LocalDateTime.now().toString();
        if (StrUtil.isEmpty(old)) {
            return StrUtil.isEmpty(input) ? null : subStr(time + " " + input, strLength);
        }
        StringBuilder builder = new StringBuilder(old);
        builder.append(System.lineSeparator());
        builder.append(time + " " + input);

        return subStr(builder.toString(), strLength);
    }

    public static String subStr(String str, int strLength) {
        if (StrUtil.isEmpty(str)) {
            return null;
        }
        if (strLength > 3 && str.length() >= strLength - 3) {
            log.warn("备注字段(str={})超过了指定的长度(length={})，被进行了截取", str, strLength - 3);
            return str.substring(0, strLength - 3) + "...";
        }
        return str;
    }


    public static void main(String[] args) {
        System.out.println(generateRemark(null, "接口报错了"));
        System.out.println(generateRemark("接口报错了", "接口成功了"));
        System.out.println(System.lineSeparator());

        System.out.println(System.lineSeparator().toString());
        System.out.println("\\r\\n");

        System.out.println(generateRemark(null, "接口报错了", 55));
        System.out.println(generateRemark("2023-07-25T16:28:30.926 接口报错了", "接口成功了", 70));
    }
}
